extends CenterContainer


var edit_data = null

onready var sprite_op = $PanelContainer/VBoxContainer/sprite/SpriteSelectPlus

onready var level_input = $PanelContainer/VBoxContainer/level/SpinBox
onready var max_count = $PanelContainer/VBoxContainer/count/SpinBox
onready var remark = $PanelContainer/VBoxContainer/remark/LineEdit
signal close


func set_edit_data(data):
	self.edit_data = data
	
	if edit_data:
		sprite_op.set_value(data["pokeId"])
		level_input.value = data["maxLevel"]
		max_count.text = data.get("maxCount",1)
		remark.text = str(data["remark"])
		

func _on_cancel_button_up():
	MainTool.getUILayer().pop_ui()


func _on_ok_button_up():
	if edit_data:
		edit_data["pokeId"] = sprite_op.get_value()
		edit_data["maxCount"] = int(max_count.value)
		edit_data["maxLevel"] = int(level_input.value)
		edit_data["remark"] = remark.text
		
		DataSource.update_data(DataSource.appoint_sprite, edit_data)
		
		MainTool.toast_info("更新成功!")
	else:
		var data = {
			"pokeId": sprite_op.get_value(),
			"maxCount": int(max_count.value),
			"maxLevel": int(level_input.value),
			"remark": remark.text,
		}
		var list = DataSource.get_data_list(DataSource.appoint_sprite)
		for item in list:
			if item["pokeId"] == data["pokeId"]:
				MainTool.toast_info("不能重复添加!")
				return
		DataSource.add_data(DataSource.appoint_sprite, data)
	
		MainTool.toast_info("添加成功!")
	
	MainTool.getUILayer().pop_ui()
	
	emit_signal("close")
